home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap3 / 3_2 / testgrid.pl < prev    next >
Encoding:
Perl Script  |  1996-06-15  |  3.2 KB  |  133 lines

  1. #!/usr/bin/perl
  2.  
  3. # Check whether the coordinates were received.
  4.  
  5. if ($#ARGV != 0)
  6. {
  7.     &CgiError("Wrong number of arguments, client may not support ISMAP.");
  8. }
  9.  
  10. $query = $ARGV[0];
  11.  
  12. # Decode the coordinates by splitting the string up into an x part and a y part
  13. # separated by a comma.
  14.  
  15. ($x, $y) = split(/,/, $query);
  16.  
  17. # Test whether the coordinates were received.
  18.  
  19. if ($y eq '')
  20. {
  21.     &CgiError("Your client doesn't support image mapping properly.");
  22. }
  23.  
  24. # Test if the coordinates are valid. Some browsers actually return negative
  25. # numbers for coordinates which is a bug in the browser software and we must
  26. # reject the input. This does not happen very often but it may happen with
  27. # users having older browsers, so be wary of the browser input.
  28.  
  29. if ($x < 0 || $y < 0)
  30. {
  31.     &CgiError("Your browser has returned negative coordinates, which is an 
  32. error. Try upgrading your browser to the latest version.");
  33. }
  34.  
  35. # Check the image size encoded in PATH_INFO. First assign the environment
  36. # variable to a local variable.
  37.  
  38. $path_info = $ENV{'PATH_INFO'};
  39.  
  40. # Parse out the cellsize parameter where cellsize equals the size of each cell
  41. # (width and height) and assign this value to the variable called $cellsize.
  42.  
  43. $path_info =~ /cellsize=(\d+)/i;
  44. $cellsize =  $1;
  45.  
  46. # Parse out numcols parameter where numcols equals the number of columns
  47. # in the image and assign this value to the $numcols variable.
  48.  
  49. $path_info =~ /numcols=(\d+)/i;
  50. $numcols =  $1;
  51.  
  52. # Test whether the image size was been defined which means $numcols and $cellsize are
  53. # both defined and non-zero. Otherwise call the CgiError function to print an error message
  54. # and exit the program.
  55.  
  56. unless ($numcols && $cellsize)
  57. {
  58.     &CgiError("Unable to determine image size from request");
  59. }
  60.  
  61. # Print the Mime type to tell your browser to expect HTML output.
  62.  
  63. print "Content-type: text/html\n\n";
  64.  
  65. # Print the required HTML tags.
  66.  
  67. print <<EOH;
  68. <HTML>
  69. <HEAD>
  70. <TITLE>CGI Script How-To: Test Script</TITLE>
  71. </HEAD>
  72. <BODY>
  73. <H1>CGI Script How-to: Test Script</H1>
  74. EOH
  75.  
  76. # Calculate the row and column numbers from the coordinates
  77.  
  78. $col = int($x / $cellsize) + 1;
  79. $row = int($y / $cellsize) + 1;
  80.  
  81. # Figure out the cell number given the row and column numbers just computed
  82. # and the number of columns.
  83.  
  84. $cellnum = ($row - 1) * $numcols + $col;
  85.  
  86. # Identify the user's selection with the (x,y) coordinates,
  87. # row number, column number, and cell number
  88.  
  89. print "You clicked on the region: <b>x=$x, y=$y</b><P>\n";
  90. print "This maps to row <b>#$row</b> and column <b>#$col</b><P>\n";
  91. print "The number of this cell is <b>$cellnum</b>\n";
  92.  
  93. # Print the final elements of the HTML output and exit
  94.  
  95. print "</BODY></HTML>\n";
  96. exit(0);
  97.  
  98. # CgiError routine displays an HTML page with an error message and exits
  99.  
  100. sub CgiError
  101. {
  102.     # Declare local variable to store the argument
  103.     local($msg) = @_;
  104.  
  105. # Output the HTML mime type. This tells the browser to expect HTML input.
  106.  
  107.     print "Content-type: text/html\n\n";
  108.  
  109. # Output an HTML document with the error message.
  110.  
  111.     print <<EOH;
  112. <HTML>
  113. <HEAD>
  114. <TITLE>Image Mapping Error</TITLE>
  115. </HEAD>
  116. <BODY>
  117. <H1>Image Mapping Error</H1>
  118. This CGI program encountered an error:
  119. <P>
  120. $msg
  121. </BODY>
  122. </HTML>
  123. EOH
  124.  
  125. # Exit the program with an non-zero error code.
  126.  
  127.     exit(1);
  128. }
  129.  
  130. ##
  131. ## end of testgrid.pl
  132. ##
  133.